import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		store: {
			name: "校园六号店",
			distance_text: 123,
			min_price: 20,
			store_address: "鼓楼区软件大道",
			work_time: "7:00 - 22:00",
			announcement: "本店食材都是通过朴朴选购，请放心食用。高峰期请提早订餐，谢谢。"
		},
		token: "",
		orderType: "takeout", //  订单类型:	takein | takeout  (default)
		member: {},
		addressList: [],
		addressEditId: 0, // 修改地址页面传值
		addressDefaultId: null,
		addressReceiver: {
			receiverDetailAddresss: "",
			receiverName: "",
			receiverPhone: ""
		},
		goods: [],
		alipayWap: "", // 阿里支付 form html 格式

		showModel: false, // 显示 购物车详情
	},
	getters: {
		// true or false 判断 是否登录
		isLogin: state => Object.keys(state.member).length > 0,
		hasReceiverAddress: state => state.addressDefaultId ? true : false,
		// 通过商品 id 获取商品img
		getProduct: (state) => {
			return (id) => {
				return state.goods.find(good => good.id == id)
			}
		},
	},
	// 同步执行的方法
	mutations: {
		SET_SHOWMODEL(state, val) {
			console.log('val',val);
			state.showModel = val;
		},
		SET_MEMBER(state, user) {
			state.member = user;
		},
		// 虚假的用户信息-Test 
		SET_MEMBER_TEST(state) {
			state.member = {
				username: "test",
				phone: "15059536516",
				headimg: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603791510546&di=def12e8433dee796578b2f352258961b&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2Fc0a20ea588b0c579d6db9d4ef71bc1554f1a5db4.jpg",
			}
		},
		REMOVE_MEMBER(state) {
			state.member = {}
		},
		SET_ORDER_TYPE(state, type) {
			state.orderType = type;
		},
		SET_USER_ADDRESS(state, addresslist) {
			state.addressList = addresslist;
		},
		SET_DUFAULT_ADDRESS(state, id) {
			state.addressDefaultId = id;
		},
		SET_RECEIVER_ADDRESS(state, id) {
			let _address = state.addressList.filter((item) => item.id == id)[0]
			state.addressReceiver.receiverDetailAddresss = _address.detailAddress;
			state.addressReceiver.receiverName = _address.name;
			state.addressReceiver.receiverPhone = _address.phone;
		},
		// 设置pay 页面的商品
		SET_GOODS(state, good) {
			// good 一个商品对象  添加 到 goods商品列表
			state.goods.push(good)
		},
		USER_QUIT(state) {
			state.token = "";
			state.member = {};
			state.addressReceiver = {
				receiverDetailAddresss: "",
				receiverName: "",
				receiverPhone: ""
			}
			state.addressList = [];
			state.addressDefaultId = null;
		}

	},
	actions: {
		async getStore({
			commit
		}) {
			// const store = await api('store')
			// commit('SET_STORE', store)
		},
		storeSetMember({
			commit
		}) {
			commit('SET_MEMBER_TEST');
		}
	}
})

export default store